<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户管理 - 管理后台</title>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.min.css">
</head>
<body>
    <div th:replace="fragments/header :: navbar"></div>
    <div class="container mt-5">
        <h2 class="mb-4"><i class="fas fa-users"></i> 用户管理</h2>
        <table class="table table-bordered table-hover">
            <thead class="thead-light">
                <tr>
                    <th>ID</th>
                    <th>用户名</th>
                    <th>邮箱</th>
                    <th>角色</th>
                    <th>状态</th>
                    <th>注册时间</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="user : ${userList}">
                    <td th:text="${user.id}"></td>
                    <td th:text="${user.username}"></td>
                    <td th:text="${user.email}"></td>
                    <td th:text="${user.role}"></td>
                    <td>
                        <span th:text="${user.status == 1 ? '正常' : '禁用'}"
                              th:classappend="${user.status == 1 ? 'text-success' : 'text-danger'}"></span>
                        <button th:if="${user.status == 1}" class="btn btn-sm btn-danger ml-2"
                                th:onclick="'updateUserStatus(' + ${user.id} + ',0)'">禁止登录</button>
                        <button th:if="${user.status == 0}" class="btn btn-sm btn-success ml-2"
                                th:onclick="'updateUserStatus(' + ${user.id} + ',1)'">恢复登录</button>
                    </td>
                    <td th:text="${#dates.format(user.registerTime, 'yyyy-MM-dd HH:mm')}"></td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
    function updateUserStatus(userId, status) {
        let msg = status === 0 ? '确定要禁止该用户登录吗？' : '确定要恢复该用户登录吗？';
        if (!confirm(msg)) return;
        fetch('/booktrading/admin/user/status', {
            method: 'POST',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            body: 'userId=' + userId + '&status=' + status
        }).then(res => res.text()).then(msg => {
            if (msg === 'success') {
                alert('操作成功');
                location.reload();
            } else {
                alert('操作失败');
            }
        });
    }
    </script>
</body>
</html> 